package com.d6game.purchase.util;

import java.security.MessageDigest;


public class MD5 {

	
	public static String encrypt(String source){
		try{
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(source.getBytes("UTF-8"));
			byte[] bytes = md.digest();
			char[] chars = new char[16*2];
			char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',  'e', 'f'};			
			int k=0;
			for(int i=0;i<16;i++){
				byte byte0 = bytes[i];                 
			    chars[k++] = hexDigits[byte0 >>> 4 & 0xf];		                      
			    chars[k++] = hexDigits[byte0 & 0xf];			    
			}
			return new String(chars);		
		}
		catch(Exception e){
			return null;
		}
	}
	public static byte[] hexStr2ByteArr(String strIn) throws Exception {  
        byte[] arrB = strIn.getBytes();  
        int iLen = arrB.length;  
        byte[] arrOut = new byte[iLen / 2];  
        for (int i = 0; i < iLen; i = i + 2) {  
            String strTmp = new String(arrB, i, 2);  
            arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);  
        }  
        return arrOut;  
    }  
}
